package com.jzo2o.foundations.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.*;
import java.util.List;
import java.util.Objects;

/**
 * 区域服务实现
 *
 * @author L
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Resource
    private ServeMapper serveMapper;


    @Resource
    private ServeItemMapper serveItemMapper;

    @Resource
    private RegionMapper regionMapper;

    /**
     * 分页查询服务列表
     *
     * @param servePageQueryReqDTO 查询条件
     * @return 分页列表
     */
    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO servePageQueryReqDTO) {
        //调用mapper查询数据，这里由于继承了ServiceImpl<ServeMapper, Serve>，使用baseMapper相当于使用ServeMapper
        PageResult<ServeResDTO> serveResDTOPageResult = PageHelperUtils.selectPage(servePageQueryReqDTO, () -> baseMapper.queryServeListByRegionId(servePageQueryReqDTO.getRegionId()));
        return serveResDTOPageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void serveAdd(List<ServeUpsertReqDTO> dto) {
        if (Objects.isNull(dto) || dto.isEmpty()) {
            throw new RuntimeException("数据为空");
        }

        for (ServeUpsertReqDTO serveUpsertReqDTO : dto) {
            ServeItem serveItem = serveItemMapper.selectById(serveUpsertReqDTO.getServeItemId());
            if (Objects.isNull(serveItem) || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new RuntimeException("该服务未启用无法添加到区域下使用");
            }

            //3.新增服务
            Serve serve = BeanUtil.toBean(serveUpsertReqDTO, Serve.class);
            Region region = regionMapper.selectById(serveUpsertReqDTO.getRegionId());
            serve.setCityCode(region.getCityCode());
            baseMapper.insert(serve);
        }


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateShelves(Long id) {
        //这个id是服务id
        Serve serve = serveMapper.selectById( id);
        if (Objects.isNull(serve)){
            throw new RuntimeException("没用此服务");
        }
        if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()) {
            throw new RuntimeException("服务以上架");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (Objects.isNull(serveItem)){
            throw new RuntimeException("没用此服务项");
        }
        if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new RuntimeException("此服务项未启用");
        }
        Serve serve1 = new Serve();
        serve1.setSaleStatus(FoundationStatusEnum.ENABLE.getStatus());
        serveMapper.update(serve1,new LambdaQueryWrapper<Serve>().eq(Serve::getId,id));
    }

    @Override
    public void deleteServe(Long id) {
        Serve serve = serveMapper.selectById(id);
        if (serve.getSaleStatus()==FoundationStatusEnum.ENABLE.getStatus()){
            throw  new ForbiddenOperationException("服务正在启用中不能删除");
        }
        serveMapper.deleteById(id);
    }

    @Override
    public void takenOffTheShelves(Long id) {
        if (Objects.isNull(serveMapper.selectById(id))){
            throw new ForbiddenOperationException("没有此服务");
        }
        Serve serve = new Serve();
        serve.setSaleStatus(FoundationStatusEnum.DISABLE.getStatus());
        serveMapper.update(serve,new LambdaQueryWrapper<Serve>().eq(Serve::getId,id));
    }

    @Override
    public void onHo(Long id) {
        Serve serve = serveMapper.selectById(id);
        if (Objects.isNull(serve)){
            throw new ForbiddenOperationException("没有此服务项");
        }
        if (serve.getSaleStatus()!=FoundationStatusEnum.ENABLE.getStatus()){
            throw new ForbiddenOperationException("此服务没有启用");
        }

        serveMapper.update(new Serve().setIsHot(FoundationStatusEnum.DISABLE.getStatus())
                ,new LambdaQueryWrapper<Serve>().eq(Serve::getId,id));
    }


}
